package cn.iocoder.yudao.module.train.service.coursecancel;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCancelDTO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.coursecancel.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecancel.CourseCancelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.coursecancel.CourseCancelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 课程消课记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CourseCancelServiceImpl implements CourseCancelService {

    @Resource
    private CourseCancelMapper courseCancelMapper;

    @Override
    public Integer createCourseCancel(CourseCancelSaveReqVO createReqVO) {
        // 插入
        CourseCancelDO courseCancel = BeanUtils.toBean(createReqVO, CourseCancelDO.class);
        courseCancelMapper.insert(courseCancel);

        // 返回
        return courseCancel.getId();
    }

    @Override
    public void updateCourseCancel(CourseCancelSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseCancelExists(updateReqVO.getId());
        // 更新
        CourseCancelDO updateObj = BeanUtils.toBean(updateReqVO, CourseCancelDO.class);
        courseCancelMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseCancel(Integer id) {
        // 校验存在
        validateCourseCancelExists(id);
        // 删除
        courseCancelMapper.deleteById(id);
    }

    @Override
        public void deleteCourseCancelListByIds(List<Integer> ids) {
        // 删除
        courseCancelMapper.deleteByIds(ids);
        }


    private void validateCourseCancelExists(Integer id) {
        if (courseCancelMapper.selectById(id) == null) {
            throw exception(COURSE_CANCEL_NOT_EXISTS);
        }
    }

    @Override
    public CourseCancelDO getCourseCancel(Integer id) {
        return courseCancelMapper.selectById(id);
    }

    @Override
    public PageResult<CourseCancelDTO> getCourseCancelPage(CourseCancelPageReqVO pageReqVO) {
        return courseCancelMapper.selectPage(pageReqVO);
    }

}
